package com.smart.upload.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.smart.upload.bean.DomainMap;
import com.smart.upload.bean.DomainSensitiveInterface;
import com.smart.upload.service.DomainSensitiveService;
import com.smart.upload.util.UploadConfigUtils;
import com.smart.util.StringUtils;

/**
 * <b>Description:文件域名管理</b><br>
 * @author 唐海洋
 * @version 1.0 2016-8-8 下午2:24:55
 */
@Component("domainSensitiveService")
public class DomainSensitiveServiceImpl implements DomainSensitiveService{

	@Override
	public void addDomain(DomainSensitiveInterface obj) {
		
		if(obj==null) return;

		String[] domainSensitiveFileds = obj.getDomainSensitiveFileds();
		if (domainSensitiveFileds == null || domainSensitiveFileds.length < 1) {
			return;
		}
		
		for(String filed:domainSensitiveFileds){
			try {
				String filedValue = (String) PropertyUtils.getProperty(obj,filed);
				if(StringUtils.isNotBlank(filedValue)){
					PropertyUtils.setProperty(obj, filed, addDomain(filedValue));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}

	}

	@Override
	public void replaceDoamin(DomainSensitiveInterface obj) {
		
		if(obj==null) return;
		
		String[] domainSensitiveFileds = obj.getDomainSensitiveFileds();
		if (domainSensitiveFileds == null || domainSensitiveFileds.length < 1) {
			return;
		}
		
		for(String filed:domainSensitiveFileds){
			try {
				String filedValue = (String) PropertyUtils.getProperty(obj,filed);
				if(StringUtils.isNotBlank(filedValue)){
					PropertyUtils.setProperty(obj, filed, replaceDomain(filedValue));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}

	@Override
	public void addDomain(List<? extends DomainSensitiveInterface> objList) {

		if(!CollectionUtils.isEmpty(objList)){
			for(DomainSensitiveInterface domain:objList){
				addDomain(domain);
			}
		}
	}
	

	@Override
	public void replaceDoamin(List<? extends DomainSensitiveInterface> objList) {
		if(!CollectionUtils.isEmpty(objList)){
			for(DomainSensitiveInterface domain:objList){
				replaceDoamin(domain);
			}
		}
	}
	
	@Override
	public String replaceDomain(String path) {
		
		if(StringUtils.isNotBlank(path)){
			List<DomainMap> DomainMapList=UploadConfigUtils.getDomainMapList();
			if(!CollectionUtils.isEmpty(DomainMapList)){
				for(DomainMap domainMap:DomainMapList){
					path=path.replaceAll(domainMap.getDomain(),domainMap.getPlaceHolder());
				}
			}
		}
		
		return path;
	}
	
	@Override
	public String addDomain(String path) {
		
		if(StringUtils.isNotBlank(path)){
			if(StringUtils.isNotBlank(path)){
				List<DomainMap> DomainMapList=UploadConfigUtils.getDomainMapList();
				if(!CollectionUtils.isEmpty(DomainMapList)){
					for(DomainMap domainMap:DomainMapList){
						path=path.replaceAll(domainMap.getPlaceHolder(),domainMap.getDomain());
					}
				}
			}
		}
		
		return path;
	}
	
	@Override
	public void addDomain(Map<String, Object> map,String[] keys) {
		
		if(CollectionUtils.isEmpty(map) || keys==null || keys.length==0){
			return;
		}
		
		for(String key: keys){
			Object value=map.get(key);
			if(value!=null){
				map.put(key, addDomain(value.toString()));
			}
		}
	}
	
	@Override
	public void addDomain(List<Map<String, Object>> mapList,String[] keys) {
		
		if(CollectionUtils.isEmpty(mapList) || keys==null || keys.length==0){
			return;
		}
		
		for(Map<String, Object> map: mapList){
			addDomain(map,keys);
		}
	}
}
